//
//  YZGAlertView.m
//  Masonry
//
//  Created by LXY on 16/6/14.
//  Copyright © 2016年 AVGD. All rights reserved.
//

#import "MJUICustomTimeView.h"
#import "MJUICustomTimeContentView.h"

static const CGFloat contentHeight = 410.0f;

@interface MJUICustomTimeView()

@property (nonatomic, strong) MJUICustomTimeContentView *viewContent;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *timeString;
@property (nonatomic) UIDatePickerMode datePickerMode;
@property (nonatomic, strong) MJUIAlertTimeConfigure *configure;
@end

@implementation MJUICustomTimeView

+ (void)showTimeAlertViewWithTitle:(NSString *)title timeString:(NSString *)timeString configure:(MJUIAlertTimeConfigure *)configure selectIndexBlock:(void(^)(NSDate *date))selectDateBlock{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    MJUICustomTimeView *blackView = [[MJUICustomTimeView alloc] initWithFrame:keyWindow.frame];//黑色阴影
    [keyWindow addSubview:blackView];
    blackView.selectDateBlock = selectDateBlock;
    blackView.contentHeight = contentHeight;
    blackView.alertType = AlertTypeBottom;
    blackView.title = title;
    blackView.timeString = timeString;
    blackView.datePickerMode = configure.datePickerMode;
    blackView.configure = configure;

    [blackView setUI];
    [blackView show];
}
- (MJUICustomTimeContentView *)viewContent {
    if (_viewContent == nil) {
        MJWeakSelf
        _viewContent = [MJUICustomTimeContentView mj_loadNibWithOwner:self];
        _viewContent.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, contentHeight);
        [_viewContent mj_cornerWithType:MJCornerRadiusTypeTop cornerRadius:16];
        [_viewContent.viewTop.cancelButton mj_addAction:^(UIButton *button) {
            [weakSelf dismiss];
        }];
        [_viewContent.btnConfirm mj_addTapActionWithBlock:^{
            weakSelf.selectDateBlock(weakSelf.viewContent.datePicker.date);
            [weakSelf dismiss];
        }];
    }
    return _viewContent;
    
}



#pragma mark **************** init
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self addSubview:self.viewContent];
        self.contentview = self.viewContent;
    }
    return self;
}
- (void)setUI{
//    1. "en_GB"英文 24小时
//
//    2. "zh_GB"中文24小时
//
//    3. ”zh_CN“中文12小时
    self.viewContent.viewTop.titleLabel.mj_localizable = self.title;
    if(self.datePickerMode == UIDatePickerModeDate){
        self.viewContent.datePicker.datePickerMode = UIDatePickerModeDate;
        if(self.configure.isYearAndMonth){
            if (@available(iOS 17.4, *)) {
                self.viewContent.datePicker.datePickerMode = UIDatePickerModeYearAndMonth;
            } else {
                // Fallback on earlier versions
            }
        }
        
        NSString *language = LanguageHelper.shared.language;
        NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:language];
//        NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en"];
        self.viewContent.datePicker.locale = locale;
        if(self.timeString.length > 0){
            if([self.timeString isEqualToString:@"-"]){
                [self.viewContent.datePicker setDate:[self.timeString mj_stringConvertToDateByFormat:@"yyyy-MM-dd"]];
            }else{
                [self.viewContent.datePicker setDate:[self.timeString mj_stringConvertToDateByFormat:@"yyyy/MM/dd"]];
            }
        }
    }
    if(self.datePickerMode == UIDatePickerModeTime){
        self.viewContent.datePicker.datePickerMode = UIDatePickerModeTime;
//        NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh"];
//        self.viewContent.datePicker.locale = locale;
        
        if(self.timeString.length > 0){
            [self.viewContent.datePicker setDate:[self.timeString mj_stringConvertToDateByFormat:@"HH:mm"]];
           
        }
        
    }
    if(self.datePickerMode == UIDatePickerModeCountDownTimer){
        self.viewContent.datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
        NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_GB"];
        self.viewContent.datePicker.locale = locale;
        if(self.timeString.length > 0){
            [self.viewContent.datePicker setDate:[self.timeString mj_stringConvertToDateByFormat:@"HH:mm"]];
            
       
        }
    }
    if(self.configure.minuteInterval > 0){
        self.viewContent.datePicker.minuteInterval = self.configure.minuteInterval;
    }
    if(self.configure.maximumDate){
        [self.viewContent.datePicker setMaximumDate:self.configure.maximumDate];
    }

}



@end
